package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 通过用户id统计喜欢个数
     *
     * @param userId
     * @return
     */
    @Override
    public long loveCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    /**
     * 通过用户id统计粉丝个数
     *
     * @param userId
     * @return
     */
    @Override
    public long fansCount(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    /**
     * 查询我喜欢的佳人
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPageMyLove(Long userId, Long page, Long pageSize) {
        //1. 查询我userId喜欢的人
        //1.1 构建查询条件 我喜欢的人，查询UserLike表时，条件为userId=登录用户的id
        Query query = new Query(Criteria.where("userId").is(userId));
        //1.2 统计总数
        long total = mongoTemplate.count(query, UserLike.class);
        List<RecommendUser> recommendUserListSort = new ArrayList<>();
        if(total > 0){
            //设置分页参数
            query.skip((page-1)*pageSize).limit(pageSize.intValue());
            query.with(Sort.by(Sort.Order.desc("created")));
            List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
            if(!CollectionUtils.isEmpty(userLikeList)) {
                //2.再查登录用户与佳人的缘分
                //2.1 获取所有佳人id
                List<Long> likeUserIds = userLikeList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
                //2.2 构建查询推荐表中国登录用户与佳人的 查询条件
                Query recommendUserQuery = new Query(Criteria.where("toUserId").is(userId)
                        .and("userId").in(likeUserIds));
                //2.3 查登录用户与佳人的缘分
                List<RecommendUser> recommendUserList = mongoTemplate.find(recommendUserQuery, RecommendUser.class);
                // 转成map key=佳人的id, value=RecommendUser
                Map<Long, RecommendUser> recommendUserMap = recommendUserList.stream()
                        .collect(Collectors.toMap(RecommendUser::getUserId, ru -> ru));
                // 转成保留顺序的结果集
                for (UserLike userLike : userLikeList) {
                    RecommendUser ru = recommendUserMap.get(userLike.getLikeUserId());
                    recommendUserListSort.add(ru);
                }
            }
        }
        return PageResult.pageResult(page, pageSize, recommendUserListSort,total);
    }

    /**
     * 查询我的粉丝
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPageMyFans(Long userId, Long page, Long pageSize) {
        //1. 查询我的粉丝，即喜欢我的人，查UserLike表 where likeUserId=登录用户id,
        //   即我被别人喜欢
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        long total = mongoTemplate.count(query, UserLike.class);
        List<RecommendUser> recommendUserListSort = new ArrayList<>();
        if(total > 0){
            query.skip((page-1)*pageSize).limit(pageSize.intValue());
            query.with(Sort.by(Sort.Order.desc("created")));
            List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
            if(!CollectionUtils.isEmpty(userLikeList)) {
                //2.再查登录用户与粉丝的缘分
                List<Long> userIds = userLikeList.stream().map(UserLike::getUserId).collect(Collectors.toList());
                Query recommendUserQuery = new Query(Criteria.where("toUserId").is(userId)
                        .and("userId").in(userIds));
                List<RecommendUser> recommendUserList = mongoTemplate.find(recommendUserQuery, RecommendUser.class);
                // 转成map key=粉丝的id, value=RecommendUser
                Map<Long, RecommendUser> recommendUserMap = recommendUserList.stream()
                        .collect(Collectors.toMap(RecommendUser::getUserId, ru -> ru));
                // 转成保留顺序的结果集
                for (UserLike userLike : userLikeList) {
                    RecommendUser ru = recommendUserMap.get(userLike.getUserId());
                    recommendUserListSort.add(ru);
                }
            }
        }
        return PageResult.pageResult(page, pageSize, recommendUserListSort,total);
    }
}
